package sleuthkit.gui.actionlistener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;

import org.apache.log4j.Logger;

import sleuthkit.gui.MainApplication;
import sleuthkit.gui.filters.AddMemoryImageFileFilter;
import sleuthkit.gui.filters.MyFileFilter;
import sleuthkit.util.CaseManagement;
import sleuthkit.util.StaticHelper;

public class AddMemoryImageActionListener implements ActionListener {

	private AddMemoryImageFileFilter addMemoryImageFileFilter;
	private MainApplication parent;
	private Logger log = Logger.getLogger(AddMemoryImageActionListener.class);
	
	public AddMemoryImageActionListener(MainApplication parent) {
		this.parent= parent;
		StaticHelper.configLogger();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser();
		fc.setFileFilter(addMemoryImageFileFilter);
		fc.setMultiSelectionEnabled(true);
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		MyFileFilter choosenFilter = new MyFileFilter(fc.getFileFilter());
		CaseManagement cm = CaseManagement.getInstance();
		
		int returnVal = fc.showOpenDialog(parent.getFrame());
		
		if (returnVal == JFileChooser.APPROVE_OPTION) {
            File[] files = fc.getSelectedFiles();
            //This is where a real application would open the file.
            for (File file : files) {
            	if (addMemoryImageFileFilter.accept(file)){
            		if (file.isDirectory()){
            			log .debug("File is a directory.");
            			log.debug("Add content to the case " + cm.getCase());
            			cm.addMemoryImageFiles(listFiles(file,addMemoryImageFileFilter));
            		} else {

            			cm.addEvidenceFile(file);
            			log.debug("Opening: " + file.getName() + ".");

            		}
            		MainApplication.getInstance().addEvidenceFileToAddEvidenceJPanel(file,addMemoryImageFileFilter);
            	}
            }
        } else {
            log.debug("Open command cancelled by user.");
        }
	}

	private ArrayList<File>  listFiles(File file,
			AddMemoryImageFileFilter addMemoryImageFileFilter2) {
		// TODO Auto-generated method stub
		return null;
	}

}
